package com.kgc.easyshopping.controller;

import com.kgc.easyshopping.dto.BizDto;
import com.kgc.easyshopping.entity.Category;
import com.kgc.easyshopping.entity.Product;
import com.kgc.easyshopping.service.CategoryService;
import com.kgc.easyshopping.service.FileService;
import com.kgc.easyshopping.service.ProductService;
import com.kgc.easyshopping.util.BizDtoUtil;
import com.kgc.easyshopping.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 管理员请求
 */
@RestController
@RequestMapping("/eb")
public class AdminController {
    MultipartFile file;
    @Autowired
    private ProductService ps;
    @Autowired
    private CategoryService cs;
    BizDto<Product> bd = null;

    @Autowired
    private FileService fileService;

    @RequestMapping("/adminDel")
    public BizDto<Product> adminDel(@RequestParam int pid){
        bd = new BizDto<>();
        bd.setCode(ps.deleteByPrimaryKey(pid));
        if(bd.getCode()==1){
            bd.setMsg("删除成功！");
        }else{
            bd.setMsg("删除失败！");
        }
        return bd;
    }

    @RequestMapping("/getProduct")
    public BizDto getProduct(@RequestParam int pid){
        return BizDtoUtil.success(ps.selectByPrimaryKey(pid));
    }

    /**
     * 图片上传
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/upload")
    public BizDto aaa(@RequestParam MultipartFile file, HttpServletRequest request){
        return BizDtoUtil.success(fileService.uploadFile(file));
//        this.file = file;
//        //获取上传到服务器的路径
//        String path = request.getServletContext().getRealPath("upload");
//        File oldFile = new File(path);
//        if(!oldFile.exists()){
//            oldFile.mkdirs();
//        }
//        //设置上传后的文件名称
//        String name = UUID.randomUUID().toString()+"."+file.getOriginalFilename().split("\\.")[1];
//
//        System.out.println(file.getName());
    }


    @PostMapping("/updateAjax")
    public BizDto getProduct(@RequestBody Product product ){
        //wenjm
        //produce image

        // yuanlai - wenjm

        return null;
    }

    @RequestMapping("/getCategory")
    public BizDto<List<Category>> getCategory(@RequestParam("parentId") Integer parentId,@RequestParam("type") Integer type){
        BizDto<List<Category>> bd = new BizDto<List<Category>>();
        bd.setData(cs.getList(parentId,type));
        return bd;
    }

    @RequestMapping("/adminGetProduct")
    public PageInfo<Product> getInfo(@RequestParam Map<String,Object> params){
        return ps.adminGetProductInfo(params);
    }
}
